package com.cw.jeeyt.service.impl.api;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.api.ApiFacadeObjEnumBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.api.ApiFacadeObjEnumDao;
import com.cw.jeeyt.dao.model.api.ApiFacadeObjEnumDo;
import com.cw.jeeyt.service.intf.api.ApiFacadeObjEnumService;
import com.cw.jeeyt.service.converter.api.ApiFacadeObjEnumConverter;
import com.cw.lang.common.utils.IdGenerator;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 枚举对象信息表业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class ApiFacadeObjEnumServiceImpl implements ApiFacadeObjEnumService {

    private static final Logger logger = LoggerFactory.getLogger(ApiFacadeObjEnumServiceImpl.class);
    @Autowired
    private ApiFacadeObjEnumDao apiFacadeObjEnumDao;//枚举对象信息表数据库操作接口

    /**
     * 查询所有枚举对象信息表
     */
    @Override
    public List<ApiFacadeObjEnumBo> queryApiFacadeObjEnumList() throws Exception {

        return ApiFacadeObjEnumConverter.doList2BoList(apiFacadeObjEnumDao.queryApiFacadeObjEnumList());
    }

    /**
     * 分页查询枚举对象信息表
     *
     * @param apiFacadeObjEnumBo
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ApiFacadeObjEnumBo> queryByPage(ApiFacadeObjEnumBo apiFacadeObjEnumBo, Integer pageNo,
        Integer pageSize, Order order) throws Exception {
        PageInfo<ApiFacadeObjEnumDo> pageInfo = apiFacadeObjEnumDao.queryByPage(
            ApiFacadeObjEnumConverter.bo2Do(apiFacadeObjEnumBo), pageNo, pageSize, order);
        return new PageInfo<>(pageInfo, ApiFacadeObjEnumConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻枚举对象信息表信息
     *
     * @param id
     * @return
     */
    @Override
    public ApiFacadeObjEnumBo getApiFacadeObjEnumById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        return ApiFacadeObjEnumConverter.do2Bo(apiFacadeObjEnumDao.getById(id));
    }

    /**
     * 新增枚举对象信息表
     *
     * @param apiFacadeObjEnumBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long insert(ApiFacadeObjEnumBo apiFacadeObjEnumBo) throws Exception {

        if (apiFacadeObjEnumBo == null) {
            logger.error("枚举对象信息表为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        //校验枚举对象信息是否已存在
        ApiFacadeObjEnumBo checkBo = this.queryEnumByCodeAndName(apiFacadeObjEnumBo.getEnumCode(),
            apiFacadeObjEnumBo.getEnumName(), apiFacadeObjEnumBo.getEnumPackage());
        if (checkBo != null) {
            throw new Exception("要添加的枚举信息已存在，请确认。");
        }
        ApiFacadeObjEnumDo apiFacadeObjEnum = ApiFacadeObjEnumConverter.bo2Do(apiFacadeObjEnumBo);
        apiFacadeObjEnum.setId(IdGenerator.nextId());
        apiFacadeObjEnum.setCreateBy(userVo.getId());
        apiFacadeObjEnum.setCreateDate(new Date());
        apiFacadeObjEnum.setUpdateBy(userVo.getId());
        apiFacadeObjEnum.setUpdateDate(new Date());
        apiFacadeObjEnumDao.insert(apiFacadeObjEnum);
        Long id = null;
        if (null == apiFacadeObjEnum.getParentId()) {
            ApiFacadeObjEnumBo insertData = this.queryEnumByCodeAndName(apiFacadeObjEnum.getEnumCode(),
                apiFacadeObjEnum.getEnumName(), apiFacadeObjEnum.getEnumPackage());
            id = insertData.getId();
        }
        return id;
    }

    /**
     * 根据枚举编码和枚举名称查询枚举信息
     *
     * @param enumCode
     * @param enumName
     * @return
     */
    @Override
    public ApiFacadeObjEnumBo queryEnumByCodeAndName(String enumCode, String enumName, String enumPackage) {
        if (StringUtils.isEmpty(enumCode) || StringUtils.isEmpty(enumName) || StringUtils.isEmpty(enumPackage)) {
            logger.error("--枚举编码或枚举名称不能为空--");
            return null;
        }
        return ApiFacadeObjEnumConverter.do2Bo(
            apiFacadeObjEnumDao.queryEnumByCodeAndName(enumCode, enumName, enumPackage));
    }

    /**
     * 新增枚举对象信息表不为空的字段
     *
     * @param apiFacadeObjEnumBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ApiFacadeObjEnumBo apiFacadeObjEnumBo) throws Exception {

        if (apiFacadeObjEnumBo == null) {
            logger.error("枚举对象信息表为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeObjEnumDo apiFacadeObjEnum = ApiFacadeObjEnumConverter.bo2Do(apiFacadeObjEnumBo);
        apiFacadeObjEnum.setCreateBy(userVo.getId());
        apiFacadeObjEnum.setCreateDate(new Date());
        apiFacadeObjEnum.setUpdateBy(userVo.getId());
        apiFacadeObjEnum.setUpdateDate(new Date());
        apiFacadeObjEnum.setId(IdGenerator.nextId());
        Integer result = apiFacadeObjEnumDao.insertSelective(apiFacadeObjEnum);
        return result;
    }

    /**
     * 更新枚举对象信息表
     *
     * @param apiFacadeObjEnumBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ApiFacadeObjEnumBo apiFacadeObjEnumBo) throws Exception {

        if (apiFacadeObjEnumBo == null) {
            logger.error("枚举对象信息表为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeObjEnumDo apiFacadeObjEnum = ApiFacadeObjEnumConverter.bo2Do(apiFacadeObjEnumBo);
        apiFacadeObjEnum.setUpdateBy(userVo.getId());
        apiFacadeObjEnum.setUpdateDate(new Date());

        apiFacadeObjEnumDao.update(apiFacadeObjEnum);
    }

    /**
     * 更新枚举对象信息表不为空的字段
     *
     * @param apiFacadeObjEnumBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ApiFacadeObjEnumBo apiFacadeObjEnumBo) throws Exception {

        if (apiFacadeObjEnumBo == null) {
            logger.error("枚举对象信息表为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeObjEnumDo apiFacadeObjEnum = ApiFacadeObjEnumConverter.bo2Do(apiFacadeObjEnumBo);
        apiFacadeObjEnum.setUpdateBy(userVo.getId());
        apiFacadeObjEnum.setUpdateDate(new Date());

        apiFacadeObjEnumDao.updateSelective(apiFacadeObjEnum);
    }

    /**
     * 删除枚举对象信息表
     *
     * @param apiFacadeObjEnumBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ApiFacadeObjEnumBo apiFacadeObjEnumBo) throws Exception {

        if (apiFacadeObjEnumBo == null) {
            logger.error("枚举对象信息表为空");
            return;
        }
        ApiFacadeObjEnumDo apiFacadeObjEnum = ApiFacadeObjEnumConverter.bo2Do(apiFacadeObjEnumBo);
        //删除枚举的子对象信息
        boolean success = this.deleteByParentId(apiFacadeObjEnum.getId());
        if (success) {
            apiFacadeObjEnumDao.delete(apiFacadeObjEnum);
        }
    }

    /**
     * 根据父id删除枚举信息
     *
     * @param parentId
     * @return
     */
    @Override
    public boolean deleteByParentId(Long parentId) throws Exception {
        Integer result = apiFacadeObjEnumDao.deleteByParentId(parentId);
        return result > 0;
    }

    /**
     * 根据父id查询枚举对象列表
     */
    @Override
    public List<ApiFacadeObjEnumBo> queryListByParentId(Long parentId) {

        if (parentId == null) {
            logger.error("父id不能为空");
            return null;
        }
        return ApiFacadeObjEnumConverter.doList2BoList(apiFacadeObjEnumDao.queryListByParentId(parentId));
    }
}
